package com.killer.texreader.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.killer.texreader.Activity.MainActivity;
import com.killer.texreader.util.Globals;
import com.killer.texreader.util.PageDao;

import java.util.Map;

/**
 * Created by killer on 2016/4/21.
 */
public class MyView extends View {
    private String content;
    private MainActivity a;
    private float startX;
    private float nowX;
    private String preContent;
    private String nextContent;
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        a = (MainActivity) context;
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    //手指按到屏幕上
                    startX = event.getX();
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    nowX = event.getX();
                    postInvalidate();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (Math.abs(startX - nowX) >= 50) {
                        //翻页
                        if (nowX > startX) {
                            //上一页
                            if (a.getNowPager() > 1) {
                                a.setNowPager(a.getNowPager() - 1);
                                changeData();
                            }
                        }

                        if (startX > nowX) {
                            //下一页
                            if (a.getNowPager() < a.getPagenum()) {
                                a.setNowPager(a.getNowPager() + 1);
                                changeData();
                            }
                        }
                    }
                    startX = 0;
                    nowX = 0;
                    postInvalidate();
                }
                return false;
            }
        });
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void changeData() {
        Map<String, Object> txtMap = a.getTxtMap();
        content = PageDao.findPageData((int)txtMap.get("txtid"), a.getNowPager());

        //上一页数据
        if(a.getNowPager()>1) {
            preContent = PageDao.findPageData((int)txtMap.get("txtid"), a.getNowPager()-1);
        }else {
            preContent = null;
        }

        //下一页数据
        if(a.getNowPager()<a.getPagenum()) {
            nextContent = PageDao.findPageData((int)txtMap.get("txtid"), a.getNowPager()+1);
        }
        a.getHandler().sendEmptyMessage(1);
        super.postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(Globals.CHAR_SIZE);

        if (content != null){
            String [] allValues = content.split(Globals.END_FLAG);

            for (int i = 0;i<allValues.length;i++){
                for (int j = 0;j<allValues[i].length();j++){
                    if (j+i*Globals.LINE_CHAR_COUNT<content.length()){
                        canvas.drawText(String.valueOf(allValues[i].charAt(j)),
                                Globals.PAGE_SEP+j*(Globals.CHAR_SIZE+Globals.CHAR_SEP)+(nowX-startX),
                                (i+1)*(Globals.LINE_SEP+Globals.CHAR_SIZE),
                                paint);
                    }
                }
            }
        }

        //下一页
        if (startX>nowX&&nextContent != null){
            String [] allValues = nextContent.split(Globals.END_FLAG);

            for (int i = 0;i<allValues.length;i++){
                for (int j = 0;j<allValues[i].length();j++){
                    if (j+i*Globals.LINE_CHAR_COUNT<content.length()){
                        canvas.drawText(String.valueOf(allValues[i].charAt(j)),
                                Globals.PAGE_SEP+j*(Globals.CHAR_SIZE+Globals.CHAR_SEP)+(nowX-startX)+Globals.SCREEN_WIDTH,
                                (i+1)*(Globals.LINE_SEP+Globals.CHAR_SIZE),
                                paint);
                    }
                }
            }

        }

        //上一页
        if (startX<nowX&&preContent != null){
            String [] allValues = preContent.split(Globals.END_FLAG);

            for (int i = 0;i<allValues.length;i++){
                for (int j = 0;j<allValues[i].length();j++){
                    if (j+i*Globals.LINE_CHAR_COUNT<content.length()){
                        canvas.drawText(String.valueOf(allValues[i].charAt(j)),
                                Globals.PAGE_SEP+j*(Globals.CHAR_SIZE+Globals.CHAR_SEP)+(nowX-startX)-Globals.SCREEN_WIDTH,
                                (i+1)*(Globals.LINE_SEP+Globals.CHAR_SIZE),
                                paint);
                    }
                }
            }

        }


    }


}
